package com.rppowell.pixie.swing.importer;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFileChooser;

public class ImageFileSelectorPreview extends JComponent implements PropertyChangeListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	ImageIcon thumbnail = null;
	File file = null;
	
	public ImageFileSelectorPreview(JFileChooser fc) {
		setPreferredSize(new Dimension(100, 50));
		fc.addPropertyChangeListener(this);
	}
	
	public void loadImage() {
		if (file == null) {
			thumbnail = null;
		} else {
			
			// Don't use createImageIcon (which is a wrapper for getResource)
			// because the image being loaded is probably not one of the 
			// programs own resources.			
			ImageIcon tmpIcon = new ImageIcon(file.getPath());
			if (tmpIcon != null) {
				if (tmpIcon.getIconWidth() > 90) {
					thumbnail = new ImageIcon(
							tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT)
					);
				} else {
					thumbnail = tmpIcon;
				}
			}
		}
	}
	
	public void propertyChange(PropertyChangeEvent e) {
		boolean update = false;
		String propertyName = e.getPropertyName();
		
		// if the directory changed, don't show an image
		if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(propertyName)) {
			file = null;
			update = true;
		// if a file became selected, find out which one	
		} else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(propertyName)) {
			file = (File) e.getNewValue();
			update = true;
		}
		
		if (update) {
			thumbnail = null;
			if (isShowing()) {
				loadImage();
				repaint();
			}
		}
	}
	
	protected void paintComponent(Graphics g) {
		if (thumbnail == null) {
			loadImage();
		}
		if (thumbnail != null) {
			int x = getWidth()/2 - thumbnail.getIconWidth()/2;
			int y = getHeight()/2 - thumbnail.getIconHeight()/2;
			if (y < 0) { y = 0; }
			if (x < 5) { x = 5; }
			thumbnail.paintIcon(this, g, x, y);
		}
	}

}
